﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMP.ERP.Admin.ServiceReference1;

namespace CMP.ERP.Admin.Controllers
{
    public class ProcurementController : Controller
    {
        //
        // GET: /Procurement/
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        public ActionResult Procurement_Show()
        {
            ViewBag.CityID = new SelectList(client.ShowCity(), "CityID", "CityName");
            ViewBag.ProviderID = new SelectList(client.GetProviderInfo(), "ProviderID", "ProviderName");
            return View();
        }
        /// <summary>
        /// 显示采购订单信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowStockOrder()
        {
            return Json(client.StockOrderInfoShow().ToList(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 显示商品信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowGoods()
        {
            List<GoodsInfo> list = client.Goodshow().ToList();
            return Json(client.Goodshow().ToList(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 采购添加
        /// </summary>
        /// <param name="selectVale">采购商品编号</param>
        /// <param name="ProviderID">供应商</param>
        /// <param name="CityID">所在地</param>
        /// <param name="DocumentsCoding">采购订单编号</param>
        /// <param name="Remarks">采购备注</param>
        /// <param name="GoodsNum">采购商品数量</param>
        /// <param name="PurchaseOrderSheetRemarks">采购商品备注</param>
        /// <returns></returns>
        public ActionResult ProcurementAdd(string selectVale, int ProviderID, int CityID, string DocumentsCoding, string Remarks, string GoodsNum, string PurchaseOrderSheetRemarks)
        {
            //截取字符串
            string[] selectGoodsID = selectVale.Split(',');
            string[] selectGoodsNum = GoodsNum.Split(',');
            string[] selectPurchaseOrderSheetRemarks = PurchaseOrderSheetRemarks.Split(',');
            //采购订单主表数据
            StockOrderInfo s = new StockOrderInfo();
            s.ProviderID = ProviderID;
            s.CityID = CityID;
            s.DocumentsCoding = DocumentsCoding;
            s.Remarks = Remarks;
            //采购订单子表数据
            List<PurchaseOrderSheetInfo> list = new List<PurchaseOrderSheetInfo>();
            for (int i = 0; i < selectGoodsID.Length; i++)
            {
                PurchaseOrderSheetInfo p = new PurchaseOrderSheetInfo();
                p.GoodsID = Convert.ToInt32(selectGoodsID[i]);
                p.GoodsNum = Convert.ToInt32(selectGoodsNum[i]);
                p.PurchaseOrderSheetRemarks = selectPurchaseOrderSheetRemarks[i];
                list.Add(p);
            }
            List<int> listint = client.AddStockOrderInfo(s, list.ToArray()).ToList();
            int content = 0;
            foreach (int item in listint)
            {
                if (item == 1)
                {
                    content = 1;
                }
                else
                {
                    content = 0;
                    continue;
                }
            }
            if(content==1)
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        /// <summary>
        /// 获取订单子表信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetProcurementByStockOrderID(int id)
        {
            ViewBag.StockOrderID = id;
            return Json(client.GetlistPurchaseOrderSheetInfoByStockOrderID(id).ToList(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取订单主表信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetStockOrderByStockOrderID(int id)
        {
            ViewBag.CityID = new SelectList(client.ShowCity(), "CityID", "CityName",client.GetStockOrderByStockOrderID(id).CityID);
            ViewBag.ProviderID = new SelectList(client.GetProviderInfo(), "ProviderID", "ProviderName", client.GetStockOrderByStockOrderID(id).ProviderID);
            return Json(client.GetStockOrderByStockOrderID(id),JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除采购信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DelStockOrderInfoAndPurcurementByStockOrderID(int id)
        {
            List<int> listint = client.DelStockOrderInfo(id).ToList();
            int content = 0;
            foreach (int item in listint)
            {
                if (item == 1)
                {
                    content = 1;
                }
                else
                {
                    content = 0;
                    continue;
                }
            }
            if (content == 1)
            {
                return Content("<script>alert('删除成功！！！');location.href='/Procurement/Procurement_Show'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败！！！');location.href='/Procurement/Procurement_Show'</script>");
            }
        }
        /// <summary>
        /// 根据单据编码搜索
        /// </summary>
        /// <param name="DocumentsCoding"></param>
        /// <returns></returns>
        public ActionResult SearchStockOrder(string DocumentsCoding)
        {
            return Json(client.GetStockOrderByStockOrderName(DocumentsCoding).ToList(), JsonRequestBehavior.AllowGet);
        }

    }
}
